<template>
	<view>
		<form class="p-1">
			<view class="flex flex-column border-bottom">
				<input v-model="username" type="text" class="uni-input pl-4" placeholder="请输入您的账号"/>
			</view>
			<view class="flex flex-column border-bottom">
				<view class="flex align-center">
					<input v-model="password" class="uni-input pl-4" placeholder="请输入密码" :password="showPassword" />
					<text class="uni-icon pr-4" :class="[!showPassword ? 'text-primary' : '']" @click="showPassword=!showPassword">&#xe568;</text>
				</view>
			</view>
		</form>
		<view class="flex align-center justify-center text-primary p-1 font-sm">忘记密码</view>
		
		<button @click="login" hover-class="animate__animated animate__pulse" class="m-2 rounded-45 font bg-blue text-white" style="height: 65rpx;line-height: 65rpx;">登录</button>
		
		<view class="flex align-center justify-center">
			<text class="text-primary font-small text-secondary">版本：{{version.name}}</text>
		</view>
		
		<view class="flex flex-column align-center justify-center fixed-bottom pb-5">
			<image src="@/static/image/logo.png" style="width: 200rpx;height: 200rpx;" mode="aspectFit"></image>
			<text class="font-sm p-2">苦修内功 让“国潮”澎湃不息</text>
		</view>
	</view>
</template>

<script setup name="login">
import { ref } from 'vue';
import {check, json} from '@/common/js/ceuer.js'
import common from '@/common/js/common.js'

const username=ref('zhangsan');
const password=ref('');
const showPassword=ref(true);
const version=ref({name:null,code:null});

function login(){
	
	if(checkData()){
		uni.request({
			method:'POST',
			url:common.baseUrl+'/app/login',
			data:{username:username.value,password:password.value},
			header:{
				'content-type':'application/x-www-form-urlencoded'
			},
			success: (res) => {
				if(res.data.flag){
					uni.setStorage({key:'app-user',data: res.data.user,success: () => {
						uni.switchTab({url:'/pages/index'});
					}});
				}else{
					uni.showToast({
						icon:'none',title:res.data.msg
					})
				}
			},
			fail: (res) => {
				uni.showToast({icon:'none',title:res.errMsg})
			}
		})
		
	}
}

function checkData() {
	if (check.empty(username.value)){
		uni.showToast({title:'账号不能为空',icon:'none'});
		return false;
	}
	if (check.empty(password.value)){
		uni.showToast({title:'密码不能为空',icon:'none'});
		return false;
	}
	return true;//检查没有错误
}

function getVersion(){
	// #ifdef APP-PLUS
	uni.getSystemInfo({
		success: (res) => {
			version.value.name=res.appVersion;
			version.value.code=res.appVersionCode;
			
			res.version
		}
	})
	// #endif
	// #ifdef MP-WEIXIN
	version.value.name=uni.getAccountInfoSync().miniProgram.version || uni.getAccountInfoSync().miniProgram.envVersion;
	// #endif
}
getVersion();
</script>

<style>
</style>
